import { createApp } from "vue";

import AppComp from "./App.vue";

import router from "./router";

// https://www.npmjs.com/package/vite-plugin-legacy-qiankun
import type { App } from "vue";
import { createLifecyle, getMicroApp } from "vite-plugin-legacy-qiankun";
import pkg from "../package.json";

const microApp = getMicroApp(pkg.name);

let app: App;

const render = (props: any = {}) => {
  const container = props.container
    ? props.container.querySelector("#app")
    : "#app";
  app = createApp(AppComp);
  app.use(router).mount(container);
};

if (microApp.__POWERED_BY_QIANKUN__) {
  // 微应用运行
  createLifecyle(pkg.name, {
    mount(props) {
      render(props);
    },
    bootstrap() {},
    unmount() {
      app.unmount();
    },
  });
} else {
  // 独立运行
  render();
}
